package com.googlecode.pwlodars.piteststat.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;

import com.googlecode.pwlodars.piteststat.database.PitResultDao;
import com.googlecode.pwlodars.piteststat.filesystembridge.FileSystemConnector;
import com.googlecode.pwlodars.piteststat.filesystembridge.TextResourcesConnector;

@Configuration
@ImportResource(value = { "classpath:property-placeholder.xml" })
public class CommonConfig {

	@Value("${driver}")
	private String dbDriver;
	@Value("${url}")
	private String dbUrl;
	@Value("${username}")
	private String dbUsername;
	@Value("${password}")
	private String dbPassword;

	@Bean
	DataSource dataSource() {
		SingleConnectionDataSource dataSource = new SingleConnectionDataSource();
		dataSource.setUsername(dbUsername);
		dataSource.setPassword(dbPassword);
		dataSource.setDriverClassName(dbDriver);
		dataSource.setUrl(dbUrl);

		return dataSource;
	}
	
	@Bean
	@Autowired
	public JdbcTemplate jdbcTemplate(DataSource dataSource){
		return new JdbcTemplate(dataSource);
	}
	
	@Bean
	@Autowired
	public PitResultDao pitResultDao(JdbcTemplate jdbcTemplate){
		return new PitResultDao(jdbcTemplate);
	}
	
	@Bean
	public TextResourcesConnector textResourcesConnector() {
		return new TextResourcesConnector();
	}

	@Bean
	public FileSystemConnector fileSystemConnector() {
		return new FileSystemConnector();
	}
}
